package com.sf.webside.controller;
import com.sf.webside.config.Global;
import com.sf.webside.service.SysUserService;
import entity.Result;
import entity.ResultVO;
import entity.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import util.GsonUtils;
import util.ResultVOUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 统一异常处理类
 */
@ControllerAdvice
@CrossOrigin
public class BaseExceptionHandler {
    private Logger logger= LoggerFactory.getLogger(BaseExceptionHandler.class);
    @Resource
    protected Global global;
    @Autowired
    protected SysUserService sysUserService;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();        
        return new Result(false, StatusCode.ERROR, "执行出错");
    }
    /**
     * 校验token
     *
     * @return
     */
    public ResultVO checkoutToken(HttpServletRequest request) {
        //判断token是否合法
        return ResultVOUtil.success();
//        String token=request.getHeader(global.getRequestTokenKey());
//        logger.info("checkoutToken  token === " + token);
//        if(StringUtils.isEmpty(token)){
//            return ResultVOUtil.error(StatusCode.ERROR_TOKEN_NULL, "token不能为空");
//        }
//        if(token.trim().length()<0){
//            return ResultVOUtil.error(StatusCode.ERROR_TOKEN_ILLEGAL, "token不合法");
//        }
//        //Token认证
//        ResultVO resultVO = sysUserService.checkoutToken(token.trim());
//        logger.info("checkoutToken resultVO "+ GsonUtils.getInstance().toJson(resultVO));
//        return resultVO;
    }


}
